package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.usermapper.DietMapper;
import com.example.fitnessservice.module.pojo.DietFoodCategory;
import com.example.fitnessservice.module.pojo.SystemDietCookbook;
import com.example.fitnessservice.service.DietService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DietServiceImpl implements DietService{
    @Autowired
    private DietMapper dietMapper;

    /**
     * 根据分类查询食物
     * @return
     */
    @Override
    public List<DietFoodCategory> findFoodAll(Integer currentPage, Integer pageSize, String food_cate) {
        //分页插件
        PageHelper.startPage(currentPage, pageSize);

        List<DietFoodCategory> all = dietMapper.findFoodAll(food_cate);
        return all;
    }

    /**
     * 根据bmi指数查询系统食谱
     * @param bmi
     * @return
     */
    @Override
    public List<SystemDietCookbook> findSystemCookbook(Integer currentPage, Integer pageSize, BigDecimal bmi) {
        //分页查询插件
        PageHelper.startPage(currentPage, pageSize);
        //查询
        List<SystemDietCookbook> systemCoobook = dietMapper.findSystemCoobook(bmi);
        return systemCoobook;
    }

    /**
     * 根据食物名称进行模糊查询
     * @param foodName
     * @return
     */
    @Override
    public List<DietFoodCategory> findFoodByName(String foodName) {
        //分页查询插件
//        PageHelper.startPage(currentPage, pageSize);
        //查询
        List<DietFoodCategory> foodsByName = dietMapper.findFoodByName(foodName);
        return foodsByName;
    }

    /**
     * 创建一个带有用户id的空食谱
     * @param userId
     */
    @Override
    public int createUserDietCookbookEmpty(int userId) {
        int flag = dietMapper.createUserDietCookbookEmpty(userId);
        return flag;
    }

    /**
     * 向user_diet_cookbook_food表中添加早餐食物
     * @param userCookbookId
     * @param foodId
     * @return
     */
    @Override
    public int addBreakfastFood(int userCookbookId, int foodId) {
        int flag = dietMapper.addBreakfastFood(userCookbookId, foodId);
        return flag;
    }

    /**
     * 查找刚刚给用户创建的带有用户id但其余数据均为空的食谱的id
     * @return
     */
    @Override
    public int findEmptyCookbookId() {
        int id = dietMapper.findEmptyDietCookbook();
        return id;
    }

    /**
     * 向user_diet_cookbook_food表中添加午餐食物
     * @param userCookbookId
     * @param foodId
     * @return
     */
    @Override
    public int addLunchFood(int userCookbookId, int foodId) {
        int flag = dietMapper.addLunchFood(userCookbookId, foodId);
        return flag;
    }

    /**
     * 向user_diet_cookbook_food表中添加晚餐食物
     * @param userCookbookId
     * @param foodId
     * @return
     */
    @Override
    public int addDinnerFood(int userCookbookId, int foodId) {
        int flag = dietMapper.addDinnerFood(userCookbookId, foodId);
        return flag;
    }


    /**
     * 保存用户的食谱详情
     * @param userId
     * @param cookbookId
     * @return
     */
    @Override
    public int storeUserDietCookbook(Integer userId, Integer cookbookId) {
        //查询该用户下的该食谱id下的所有早餐id
        List<Integer> breakfastFoodsIds = dietMapper.findUserDietCookbookFoodBreakfast(userId, cookbookId);
        //查询该用户下的该食谱id下的所有午餐id
        List<Integer> lunchFoodsIds = dietMapper.findUserDietCookbookFoodLunch(userId, cookbookId);
        //查询该用户下的该食谱id下的所有晚餐id
        List<Integer> dinnerFoodsIds = dietMapper.findUserDietCookbookFoodDinner(userId, cookbookId);
        //分别对早、午、晚进行遍历，得到食物id，并且进行查询得到食物，进而获得食物名称
        String breakfastDetails = "早餐：";
        String lunchDetails = "午餐：";
        String dinnerDetails = "晚餐：";
        for(Integer i : breakfastFoodsIds){
            DietFoodCategory food = dietMapper.findFoodById(i);
            String foodName = food.getFoodName();
            breakfastDetails = breakfastDetails + foodName + "；";
        }
        for(Integer i : lunchFoodsIds){
            DietFoodCategory food = dietMapper.findFoodById(i);
            String foodName = food.getFoodName();
            lunchDetails = lunchDetails + foodName + "；";
        }
        for(Integer i : dinnerFoodsIds){
            DietFoodCategory food = dietMapper.findFoodById(i);
            String foodName = food.getFoodName();
            dinnerDetails = dinnerDetails + foodName + "；";
        }
        //将获得的食物名称插入到该用户的该食谱id的食谱中
        System.out.println(breakfastDetails);
        System.out.println(lunchDetails);
        System.out.println(dinnerDetails);
        int flag = dietMapper.storeUserDietCookbookDetails(userId, cookbookId, breakfastDetails, lunchDetails, dinnerDetails);
        return flag;
    }
}
